package com.iring.controller.admin;

import java.util.Collection;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.iring.controller.BaseController;
import com.iring.domain.common.User;
import com.iring.domain.vos.suite.InfoSuite;
import com.iring.vo.common.BooleanResultVO;
import com.iring.vo.common.ResultVO;
import com.iring.vo.vos.AddSuiteVO;
import com.iring.vo.vos.DeleteSuiteVO;

/**
 * 套餐controller
 *
 * @author wanggan
 */
@RestController
@RequestMapping(value = "/admin/suite", produces = { "application/json;charset=UTF-8" })
public class SuiteController extends BaseController {

    /**
     * 获取所有的套餐
     *
     * @return 显示所有的套餐信息
     */
    @RequestMapping(value = "/list-all-suite", method = RequestMethod.POST)
    public Collection<InfoSuite> listAllSuite() {
        return allSuiteService.getAllSuiteCache();
    }

    /**
     * 更新所有的套餐信息
     *
     * @return 刷新结果
     */

    @RequestMapping(value = "/refresh-all-suite", method = RequestMethod.POST)
    public ResultVO refreshAllSuite() {
        allSuiteService.cleanAllSuite();
        allSuiteService.loadAllSuite();
        return new ResultVO("1");
    }

    /**
     * 获取用户的生效套餐信息
     *
     * @return 显示所有的生效套餐信息
     */
    @RequestMapping(value = "/list-all-valid-suite", method = RequestMethod.POST)
    public String listAllPhoneSuite() {
        // 查询用户的生效套餐信息
        return packageService.listAllPhoneSuite(getUserSession().getUsername());
    }

    /**
     * 获取用户所有的订单套餐
     *
     * @return 显示所有的套餐订单信息
     */
    @RequestMapping(value = "/list-all-order-suite", method = RequestMethod.POST)
    public String listAllOrderSuite() {
        return packageService.listAllOrderSuite(getUserSession().getUsername());
    }

    /**
     * 创建套餐订单
     *
     * @param addSuiteVO
     *            // 指定发送消息
     *            {@link AddSuiteVO}
     * @return 创建结果
     */
    @RequestMapping(value = "/add-order-suite", method = RequestMethod.POST)
    public ResultVO createPackage(@RequestBody AddSuiteVO addSuiteVO) {
        String username = getUserSession().getUsername();
        User user = packageService.createSuite(addSuiteVO, username);
        logService.saveLog(username,"add-order-suite",getUserRealIp(),true);
        return new ResultVO(user.getRestAmounts());
    }

    /**
     * 删除用户套餐订单
     *
     * @param deleteSuiteVO
     *            {@link DeleteSuiteVO}
     * @return 删除套餐订单结果
     */
    @RequestMapping(value = "/delete-order-suite", method = RequestMethod.POST)
    public BooleanResultVO deleteOrderSuite(@RequestBody DeleteSuiteVO deleteSuiteVO) {
        String username = getUserSession().getUsername();
        Boolean result = packageService.deleteOrderPackage(deleteSuiteVO, username);
        logService.saveLog(username,"delete-order-suite",getUserRealIp(),true);
        return new BooleanResultVO(result);
    }

}
